﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using rmyy.IService;
using rmyy.RepositoryFactory;
using rmyy.IRepository;

namespace rmyy.Service
{
    public abstract partial class BaseService<T>  where T : class, new()
    {
        public rmyy.IRepository.IBaseRepository<T> CurrentRepository
        {
            get;set;
        }
        protected BaseService()
        {
            SetCurrentRepository();
        }
        protected abstract void SetCurrentRepository();
  
        public DBSession CurrentDBSession
        {
            get
            {
                return DBSessionFactory.CreateDBSession();
            }
        }

        public T AddEntity(T entity)
        {
            return CurrentRepository.AddEntity(entity);
        }

        public bool DelEntity(T entity)
        {
            CurrentRepository.DelEntity(entity);
            return CurrentDBSession.SaveChanges() > 0;
        }

        public bool EditEntity(T entity)
        {
            CurrentRepository.EditEntity(entity);
            return CurrentDBSession.SaveChanges()>0;
        }

        public IQueryable<T> LoadEntities(Expression<Func<T, bool>> whereLambda)
        {
            return CurrentRepository.LoadEntities(whereLambda);
        }

        public IQueryable<T> LoadPageEntities<s>(int pageIndex, int pageSize, out int totalCount, Expression<Func<T, bool>> whereLambda, Expression<Func<T, s>> orderLambda, bool isAsc)
        {
            return CurrentRepository.LoadPageEntities<s>(pageIndex, pageSize, out totalCount, whereLambda, orderLambda, isAsc);
        }
    }
}
